package com.xuyiwei.mayi.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by xuyiwei on 2019/4/13.
 */
@Table("user")
public class UserAnnotation {

    @Property(name="userId",length = 10)
    private String id;

    @Property(name="userName")
    private String name;

    public UserAnnotation(String id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "UserAnnotation{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public static void main(String[] args) {
        StringBuffer sql = new StringBuffer();
        sql.append("select  ");
        StringBuffer fromSql = new StringBuffer();
        Class<UserAnnotation> userAnnotationClass = UserAnnotation.class;
        Annotation[] alist = userAnnotationClass.getAnnotations();
        for(Annotation a:alist){
            if(a.annotationType().equals(Table.class) ){
               Table table= (Table) a;
                fromSql.append(" from " +table.value());
            }
        }
        Field[] fields = userAnnotationClass.getDeclaredFields();
        for(Field f:fields){
            Property p = f.getAnnotation(Property.class);
            String name = p.name();
            sql.append(" "+name+", ");
        }
        String sqlStr = sql.substring(0,sql.length()-2);

        System.out.println(sqlStr+fromSql.toString());
    }
}
